const head = document.getElementsByTagName("header")[0];
let data = {
  articleName: "",
  title: "",
  pic: "",
  text: "",
}

// 引入模块 Ajax
import {funt} from "./ajax.js";
// const URL = "http://localhost:3000/articles";
// const URL = "http://xuejiang.free.idcfengye.com/articles";
const URL = "	http://xuesang.vipgz1.91tunnel.com/articles";

// 文章名字
let inputs = head.querySelector(".left-title").querySelector("input");


// 提交按钮
function releases() {  
  let release = head.getElementsByClassName("right-release")[0].querySelector("span"); 
  let submitPage = head.getElementsByClassName("right-release")[0].querySelector(".submitPage");
  let k = false;
  
  release.addEventListener("click", function() {
    if(k = !k) { submitPage.className = "submitPage submitPageBlock"; }
    else { submitPage.className = "submitPage submitPageNone"; }    
  });
}
  
// 提交标题
function releaseTitles() {
  let releaseTitle = head.getElementsByClassName("right-release")[0].querySelector(".submitTitle");
  let lis = releaseTitle.querySelectorAll("li");
  // 标题
  for(let i = 0; i < lis.length; i++) {      
    lis[i].onclick = function() {
      for(var i = 0; i < lis.length; i++) { lis[i].className = ""; }
      this.className = "liSelect";
      data.title = this.innerHTML;      
    }
  }
}

// 提交图片
function releasesPicture() {  
  let files = head.getElementsByClassName("right-release")[0].querySelector(".submitPage").querySelector(".submitPicture").querySelector("input"); 
  let releasesPictures = head.getElementsByClassName("right-release")[0].querySelector(".submitPage").querySelector(".submitPicture");

  // 选择图片
  if(typeof FileReader==='undefined'){ 
    result.innerHTML = "抱歉，你的浏览器不支持 FileReader"; 
    files.setAttribute('disabled','disabled'); 
  }else{ 
    files.addEventListener('change', function (){ 
      var file = this.files[0];       
      if(!/image\/\w+/.test(file.type)){ 
          alert("文件必须为图片！"); 
          return false; 
      } 
      
      var reader = new FileReader();       
      reader.readAsDataURL(file); 
      
      // 加载图片
      const p = new Promise((resolve, reject) => {
        reader.onload = function(e){
          resolve(this.result);                    
        };      
      });
      p.then(value => {
        data.pic = value;        
        let imgs = document.createElement("img");
        imgs.src = value;
        releasesPictures.appendChild(imgs);
      }).catch(err => {
        console.log(err);
      });       
    },false); 
  } 
}

// 提交描述
function releasesSketch() {
  let www = head.getElementsByClassName("right-release")[0].querySelector(".submitPage").querySelector(".submitSketch");
  
  let text2 = www.querySelector("#text2");
  let number1 = www.querySelector(".number1");

  let str = "";
  
  text2.onkeydown = function(e) {        
    text2.onkeyup = function(e) {      
      if((text2.value).length <= 35) {
        str = text2.value;
        number1.innerHTML = (text2.value).length + "/35";    
        data.text = str;         
      }else {
        text2.value = str;        
      }
        
    }
  }
  
}

// 提交整个数据包
function getdata(outputFrame) {  
  // 两个$表示斜体
  // 1至6个#表示1到6不同大小的标题
  // 两个*表示加粗(必须在空格内)
  // 两个---->----图形表示代码块
  // 无序列表 1至n
  // 有序列表 ·
  // console.log(outputFrame);
  // 获取描述元素
  let www = head.getElementsByClassName("right-release")[0].querySelector(".submitPage").querySelector(".submitSketch");
  let text2 = www.querySelector("#text2");

  let submitSend = head.getElementsByClassName("right-release")[0].querySelector(".submitPage").querySelector(".submitSend");  
  // 获取提交页面元素
  let submitPage = head.getElementsByClassName("right-release")[0].querySelector(".submitPage");
  submitSend.onclick = function() {     
    // 关闭提提交页面
    submitPage.className = "submitPage submitPageNone";
    // 标题赋值
    data.articleName = inputs.value;

    // 提交
    if(outputFrame != "" && outputFrame.innerHTML != "") {          
      if(data.articleName && data.title != "" && data.pic != "" && data.text != "") {                
        funt("post", URL, undefined, 
             `outputFrame=${outputFrame.innerHTML}&articleName=${data.articleName}&title=${data.title}&pic=${data.pic}&text=${text2.value}&thumb_up=0&browse=0&speak=Array&namev=a-0-a`);     
        setTimeout(() => {
          window.location.href="../index.html";
        }, 2000);        
      }else {
        alert("要填写好必填的信息！");
      }
    }else {
      alert("要填写好必填的信息！");
    } 
  }       
}

// 提交
getdata("");

// 提交描述
releasesSketch();
// 提交按钮
releases();
// 提交标题
releaseTitles();
// 提交图片
releasesPicture();


export {
  getdata,
}